namespace OdeToFood.Migrations
{
    using OdeToFood.Models;
    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using System.Web.Security;
    using WebMatrix.WebData;

    internal sealed class Configuration : DbMigrationsConfiguration<OdeToFood.Models.OdeToFoodDb>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled =false;
        }

        protected override void Seed(OdeToFood.Models.OdeToFoodDb context)
        {
            context.Restaurants.AddOrUpdate(r => r.Name, 

                new Restaurant { Name ="Sabatino'S " , City = "Baltimore" , Country = "USA"},
               
                new Restaurant { Name ="Kappy's " , City = "Oakbrook" , Country = "USA"},
                 new Restaurant { Name ="Danny'S " , City = "Villa Park" , Country = "USA"},
                 new Restaurant { Name ="Clara'S " , City = "Woodridge" , Country = "USA", Reviews=   new List<RestaurantReview> {
                     new RestaurantReview {Rating =9, Body="Great Food!"}
                 }

                 });
 
            SeedMembership();


            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }

        private void SeedMembership()
        {
            if (!WebSecurity.Initialized)
            {


                WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);

            }
            var roles = (SimpleRoleProvider)Roles.Provider;
            var membership = (SimpleMembershipProvider)Membership.Provider;

            if (!roles.RoleExists("Admin"))
            {
                roles.CreateRole("Admin");
            }
            if (membership.GetUser("Alek", false)==null)

            {
                membership.CreateUserAndAccount("Alek", "mkd337262");


            }

            if (!roles.GetRolesForUser("Alek").Contains("Admin"))
            {
                roles.AddUsersToRoles(new[] {"Alek"}, new[]{"Admin"});
            }
        }



       

    }
}
